import java.util.Timer;
import java.util.TimerTask;

public class ControlPanel {
    private static final String CORRECT_PASSWORD = "1234"; // 假设正确密码是"1234"
    private static final int MAX_ATTEMPTS = 3; // 最大输入错误次数
    private static final int LOCK_TIME = 120 * 1000; // 锁定时间为120秒
    private static final int PASSWORD_LENGTH = 4; // 密码长度为4位

    private enum State {
        READING_INPUT,       // 读取输入状态
        VALIDATING_PASSWORD, // 验证密码状态
        LOCKED,              // 锁定状态
        SYSTEM_FUNCTION,     // 系统功能选择状态
    }

    private State currentState;
    private StringBuilder currentPassword;
    private int incorrectAttempts;
    private Timer lockTimer;

    public ControlPanel() {
        currentState = State.READING_INPUT;
        currentPassword = new StringBuilder();
        incorrectAttempts = 0;
        lockTimer = new Timer();
    }

    // 模拟户主敲击控制面板键盘按键输入密码
    public void inputCharacter(char character) {
        switch (currentState) {
            case READING_INPUT:
                currentPassword.append(character);
                System.out.println("输入的密码：" + currentPassword.toString());

                if (currentPassword.length() == PASSWORD_LENGTH) {
                    currentState = State.VALIDATING_PASSWORD;
                    validatePassword();
                }
                break;
            case LOCKED:
                System.out.println("控制面板被锁定，请稍后再试！");
                break;
            case SYSTEM_FUNCTION:
                System.out.println("已进入系统功能选择状态。");
                break;
        }
    }

    // 验证密码是否正确
    private void validatePassword() {
        if (currentPassword.toString().equals(CORRECT_PASSWORD)) {
            System.out.println("密码正确，进入系统功能选择状态。");
            currentState = State.SYSTEM_FUNCTION;
        } else {
            incorrectAttempts++;
            System.out.println("密码错误，尝试次数：" + incorrectAttempts);
            if (incorrectAttempts >= MAX_ATTEMPTS) {
                currentState = State.LOCKED;
                System.out.println("输入错误3次，控制面板已锁定！");
                startLockTimer();
            } else {
                currentState = State.READING_INPUT; // 继续读取输入
                currentPassword.setLength(0); // 清空当前输入的密码
            }
        }
    }

    // 启动锁定计时器，120秒后解锁
    private void startLockTimer() {
        lockTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("锁定时间结束，可以重新输入密码了！");
                currentState = State.READING_INPUT; // 重新进入输入状态
                incorrectAttempts = 0;
                currentPassword.setLength(0); // 清空当前输入的密码
            }
        }, LOCK_TIME);
    }

    public static void main(String[] args) {
        ControlPanel panel = new ControlPanel();

        // 模拟用户输入密码
        panel.inputCharacter('1');
        panel.inputCharacter('2');
        panel.inputCharacter('3');
        panel.inputCharacter('5'); // 密码错误

        panel.inputCharacter('1');
        panel.inputCharacter('2');
        panel.inputCharacter('3');
        panel.inputCharacter('6'); // 密码错误

        panel.inputCharacter('1');
        panel.inputCharacter('2');
        panel.inputCharacter('3');
        panel.inputCharacter('7'); // 密码错误，控制面板锁定

        // 模拟锁定结束后的输入
        // 120秒后，自动解锁并可以继续输入
    }
}
